//
//  CJNativeGMAdapter.m
//  CJModileGMAdapter
//
//  Created by shanghailingye on 2024/4/24.
//

#import "CJNativeToBidAdapter.h"

@interface CJNativeToBidAdapter()<CJNativeAdDelegate>

@property (nonatomic, weak) id<AWMCustomNativeAdapterBridge> bridge;
@property (nonatomic, strong) CJNativeAd *nativeAd;
// 是否是bidding
@property (nonatomic, assign) BOOL isBidding;

@end

@implementation CJNativeToBidAdapter

- (instancetype)initWithBridge:(id<AWMCustomNativeAdapterBridge>)bridge {
    self = [super init];
    if (self) {
       _bridge = bridge;
    }
    return self;
}

- (void)loadAdWithPlacementId:(NSString *)placementId adSize:(CGSize)size parameter:(AWMParameter *)parameter {
    self.nativeAd = [[CJNativeAd alloc] initWithSlotId:placementId size:size rootViewController:[CJCommonToBidFunc currentRootController]];
    self.nativeAd.delegate = self;
    [self.nativeAd loadAdData:1];
}

- (BOOL)mediatedAdStatus {
    return true;
}


#pragma mark CJNativeAdDelegate

- (void)nativeExpressAdDidLoad:(nonnull NSArray *)nativeExpressViews {
    NSInteger cpm = [self.nativeAd getECpm];
    NSDictionary *ext = @{
        AWMMediaAdLoadingExtECPM : cpm ? [NSString stringWithFormat:@"%ld", cpm] : @"0",
    };
    if (self.isBidding) { [self.bridge nativeAd:self didAdServerResponseWithExt:ext]; }
    [self.bridge nativeAd:self didLoadWithNativeAds:nativeExpressViews];
    if (nativeExpressViews.count > 0 && [nativeExpressViews.firstObject isKindOfClass:[UIView class]]) {
        [self.bridge nativeAd:self renderSuccessWithExpressView:nativeExpressViews.firstObject];
    }
}

- (void)nativeExpressAdDidRender:(nonnull id)nativeExpressView {
    
}

- (void)nativeExpressAdLoadFailed:(nonnull id)nativeExpressAd error:(nonnull NSError *)error {
    [self.bridge nativeAd:self didLoadFailWithError:error];
}

- (void)nativeExpressAdOnClicked:(nonnull id)nativeExpressView {
    [self.bridge nativeAd:self didClickWithMediatedNativeAd:nativeExpressView];
}

- (void)nativeExpressAdOnClosed:(nonnull id)nativeExpressView {
    [self.bridge nativeAd:self didClose:nativeExpressView closeReasons:nil];
}

- (void)nativeExpressAdOnShow:(nonnull id)nativeExpressView {
    [self.bridge nativeAd:self didVisibleWithMediatedNativeAd:nativeExpressView];
}
@end
